import { defineStore } from 'pinia'
import { ref } from 'vue'

type OnNeedPrivacyAuthorizationResolve = (params : { buttonId ?: string; event : 'agree' | 'disagree' }) => void;
const privacyResolveList = new Set<OnNeedPrivacyAuthorizationResolve>();
// 定义 Store
export const usePrivacyStore = defineStore(
	'privacy',
	() => {

		const privacyModal = ref({
			show: false,
			resolve: () => { }
		})

		function show(show = true) {
			privacyModal.value.show = show;
		}

		function addResolveAndShow(resolve : () => void) {
			// privacyModal.value.resolve = resolve;
			show(true);
			privacyResolveList.add(resolve);
		}

		function disagree() {
			show(false);
			for (const resolve of privacyResolveList) {
				resolve({ event: 'disagree', buttonId: 'disagree-button' });
			}
			privacyResolveList.clear();
		}

		function agree() {
			show(false);
			for (const resolve of privacyResolveList) {
				resolve({ event: 'agree', buttonId: 'agree-button' });
			}
			privacyResolveList.clear();
		}

		// 记得 return
		return {
			show,
			privacyModal,
			addResolveAndShow,
			disagree,
			agree
		}
	}
)